package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ContractProductDao;
import cn.itcast.dao.cargo.ExtCproductDao;
import cn.itcast.dao.cargo.FactoryDao;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ContractProductService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ContractProductServiceImpl implements ContractProductService {
    @Autowired
    private ContractProductDao contractProductDao;
    @Autowired
    ContractDao contractDao;
    @Autowired
    ExtCproductDao excproductDao;
    @Autowired
    FactoryDao factoryDao;
    @Override
    public void save(ContractProduct contractProduct) {
          /*保存货物：
        1.设置货物id   在Controller已经设置完毕
        2.计算货物总金额     小计金额=价格*数量
        3.设置货物的总金额
        4.保存货物
        5.根据id查询购销合同
        6.设置购销合同总金额
        7.设置购销合同货物数量
        8.更新购销合同*/

        Double price = contractProduct.getPrice();
        Integer cnumber = contractProduct.getCnumber();
//  2.计算货物总金额     小计金额=价格*数量
        double amount = price * cnumber;
//        3.设置货物的总金额
        contractProduct.setAmount(amount);
//        4.保存货物
        contractProductDao.insertSelective(contractProduct);


//       5.根据id查询购销合同
        String contractId = contractProduct.getContractId();
        Contract contract = contractDao.selectByPrimaryKey(contractId);
//        对购销合同的数据修改 货物的数量+1  总金额+这批货物的小计金额
//        6.设置购销合同总金额
        contract.setTotalAmount(contract.getTotalAmount()+amount);
//        7.设置购销合同货物数量
        contract.setProNum(contract.getProNum()+1);
//        8.更新购销合同
        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override
    public void update(ContractProduct contractProduct) {
        Double price = contractProduct.getPrice();
        Integer cnumber = contractProduct.getCnumber();
//      重新计算货物的小计金额
        double amount_new = price * cnumber;
//        设置货物的小计金额
        contractProduct.setAmount(amount_new);

//        获取旧的小计金额
//        数据库中的旧的小计金额还没有发生变化
//        查询原来的小计金额的数据
        ContractProduct contractProduct_old = contractProductDao.selectByPrimaryKey(contractProduct.getId());
        Double amount_old = contractProduct_old.getAmount();

        String contractId = contractProduct.getContractId();
//        查询合同
        Contract contract = contractDao.selectByPrimaryKey(contractId);
//        重新设置合同的总金额
        contract.setTotalAmount(contract.getTotalAmount()-amount_old+amount_new);
//        更新合同
        contractDao.updateByPrimaryKeySelective(contract);

//        更新货物
        contractProductDao.updateByPrimaryKeySelective(contractProduct);
    }

    @Override
    public void delete(String id) {
        ContractProduct contractProduct = contractProductDao.selectByPrimaryKey(id);
        Double amount = contractProduct.getAmount();
        List<ExtCproduct> extCproducts = contractProduct.getExtCproducts();
        Double exTotalAmount=0.0;
        for (ExtCproduct extCproduct : extCproducts) {
            exTotalAmount+=extCproduct.getAmount();
            excproductDao.deleteByPrimaryKey(extCproduct.getId());
        }
        int extSize = extCproducts.size();
        Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());
        contract.setProNum(contract.getProNum()-1);
        contract.setExtNum(contract.getExtNum()-extSize);
        contract.setTotalAmount(contract.getTotalAmount()-amount-exTotalAmount);
        contractDao.updateByPrimaryKeySelective(contract);
        contractProductDao.deleteByPrimaryKey(id);

    }

    @Override
    public ContractProduct findById(String id) {

        return contractProductDao.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo findAll(ContractProductExample example, int page, int size) {
        PageHelper.startPage(page,size);
        List<ContractProduct> list = contractProductDao.selectByExample(example);

        return new PageInfo(list);
    }

    @Override
    @Transactional
    public void saveList(List<ContractProduct> contractProducts) {
        for (ContractProduct contractProduct : contractProducts) {
            FactoryExample example = new FactoryExample();
            example.createCriteria().andFactoryNameEqualTo(contractProduct.getFactoryName()).andCtypeEqualTo("货物");
            List<Factory> factoryList = factoryDao.selectByExample(example);
            if (factoryList!=null&&factoryList.size()>0){
                contractProduct.setFactoryId(factoryList.get(0).getId());
            }
            this.save(contractProduct);
        }
    }
}
